<?php

namespace App\Kuafu\Dashboard\Controller\Pos;

use App\Kuafu\Banner\Enum\BannerEnum;
use App\Kuafu\Banner\Service\BannerCommonService;
use App\Kuafu\Brand\Service\BrandService;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Dashboard\Enum\DashboardCacheCodeEnum;
use App\Kuafu\Dashboard\Service\PosOnlineService;
use App\Kuafu\Shop\Enum\CodeEnum as ShopEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\User\Service\AuthService;
use App\Library\KfHttp\TigerCurl;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use Illuminate\Support\Facades\Redis;

class DashboardController extends BaseController
{
    /**
     * 展示店铺列表
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public function shopList()
    {
        $userId   = AuthService::getUserId();
        $platform = CodeEnum::PLATFORM_POS;

        // 获取当前用户有的店铺列表
        $shopList = ShopService::getShopListByUserAndPlatform($userId, $platform,
            [ShopEnum::SHOP_STATUS_NORMAL, ShopEnum::SHOP_STATUS_PREPARE]
        );

        $brandList = [];
        // 获取门店的品牌 Id
        if (!empty($shopList)) {
            $brandIds  = array_column($shopList, 'brandId');
            $brandList = BrandService::getBrandListByIds($brandIds);
            $brandList = collect($brandList)->map(function ($v) {
                $v['imageUrl'] = CommonService::joinImageUrl($v['imageUrl']);
                return $v;
            })->toArray();

            $brandList = array_column($brandList, null, 'id');

            // 管理费缴纳情况
            $shopIds       = array_column($shopList, 'id');
            $payStatusList = TigerCurl::getIns()
                                      ->do('callback/fee/payStatus', ['shopIds' => $shopIds],
                                          'GET')['data']['pay'] ?? [];

            foreach ($shopList as $shop) {
                $shop['shopCode']     = str_pad($shop['shopCode'], ShopCodeEnum::CODE_TEXT[ShopCodeEnum::LEADING_ZERO],
                    "0",
                    STR_PAD_LEFT);
                $shop['annualFeePay'] = $payStatusList[$shop['id']] ?? 1;


                if (!empty($brandList[$shop['brandId']])) {
                    $brandList[$shop['brandId']]['shopList'][] = $shop;
                }
            }
            $brandList = array_values($brandList);
        }

        $return = [
            'list' => $brandList
        ];

        return $this->success($return);
    }

    public function getAuxiliaryScreenRotation(Request $request): \Illuminate\Http\JsonResponse
    {
        $shopId   = $this->shopId;
        $shopInfo = ShopService::getShopInfo($shopId);
        $type     = BannerEnum::TYPE_AUXILIARY_SCREEN;
        $field    = ['img', 'extraImg', 'name'];
        $list     = BannerCommonService::getAuxiliaryScreenRotation($type, $shopId, $shopInfo['type'], $field, 'get');
        return $this->success(['list' => $list]);
    }


    /**
     * 支付状态
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function onlineStatus(Request $request): JsonResponse
    {

        $result = [
            'onlineStatus' => true
        ];

        // 延长 POS 在线时间
        PosOnlineService::setPosOnlineStatusTime($this->shopId);

        $config = ConfigModel::getDataPageByType('posStatus');

        if (!empty($config)) {
            $config = $config[0];

            $config = json_decode($config['value'], 1);

            if (!empty($config)) {
                $result = $config;
            }

        }
        return $this->success($result);
    }

}
